package com.atguigu.gulimail.controller;

import com.atguigu.gulimail.service.CartService;
import com.atguigu.gulimail.vo.Cart;
import com.atguigu.gulimail.vo.CartItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * @author fangy
 * @date 2022-10-11 21:53
 */
@Controller
public class indexController {

    @Autowired
    CartService cartService;


    @RequestMapping("/cart.html")
    public String index(Model model){


        Cart cart = cartService.getCart();

        model.addAttribute("cart",cart);

        return "cartList";

    }

    @RequestMapping("/addToCart")
    public String addToCart(@RequestParam("skuId") Long skuId,
                            @RequestParam("num") Integer num,
                            RedirectAttributes redirectAttributes){

        cartService.addToCart(skuId,num);

        redirectAttributes.addAttribute("skuId",skuId);

        return "redirect:http://cart.gulimail.com/addToCartSuccess.html";
    }

    @RequestMapping("/addToCartSuccess.html")
    public String addToCartSuccess(@RequestParam("skuId") Long skuId,Model model){

        CartItem cartItem = cartService.getCartItem(skuId);
        model.addAttribute("item",cartItem);

        return "success";
    }
    
    @RequestMapping("/checkItem")
    public String checkItem(@RequestParam("skuId") String skuId,@RequestParam("check")int check){
        Long l = Long.parseLong(skuId);
        cartService.checkItem(l,check);
        return "redirect:http://cart.gulimail.com/cart.html";
    }

    @RequestMapping("/deleteItem")
    public String deleteItem(@RequestParam("skuId") String skuId){
        cartService.deleteItem(skuId);
        return "redirect:http://cart.gulimail.com/cart.html";
    }

}
